import utils
from manage import ip_controller


def add_slave(args):
    if len(args) != 1:
        print("args wrong format")
    else:
        ip_controller.add_slave_ip(args[0])


def delete_slave(args):
    if len(args) != 1:
        print("args wrong format")
    else:
        ip_controller.delete_slave_ip(args[0])


def add_head_up(args):
    if len(args) != 1:
        print("args wrong format")
    else:
        ip_controller.add_head_up_ip(args[0])


def add_head_down(args):
    if len(args) != 1:
        print("args wrong format")
    else:
        ip_controller.add_head_down_ip(args[0])


def delete_head_down(args):
    if len(args) != 1:
        print("args wrong format")
    else:
        ip_controller.delete_head_down_ip(args[0])


def add_master(args):
    if len(args) != 1:
        print("args wrong format")
    else:
        ip_controller.add_master_ip(args[0])


def get_up_ip(mode):
    lines = []
    if mode == "slave":
        lines = utils.read_data("/collector_slave/config/ips/ips_head_up.conf")
    elif mode == "head":
        lines = utils.read_data("/collector_slave/config/ips/ips_master.conf")
    if lines:
        return lines[0].rstrip("\n")


def get_down_ip(mode):
    lines = []
    if mode == "master":
        lines = utils.read_data("/collector_slave/config/ips/ips_head_down.conf")
    elif mode == "head":
        lines = utils.read_data("/collector_slave/config/ips/ips_slave.conf")
    if lines:
        return lines


def check_down_ip(mode, ips):
    lines = []
    add = []
    red = []
    if mode == "head":
        lines = utils.read_data("/collector_slave/config/ips/ips_slave.conf")
    elif mode == "master":
        lines = utils.read_data("/collector_slave/config/ips/ips_head_down.conf")
    for ip in ips:
        if ip + "\n" not in lines:
            add.append(ip)
    for line in lines:
        if line.rstrip("\n") not in ips:
            red.append(line.rstrip("\n"))
    return add, red


def set_stop_signal(ip):
    utils.set_redis(ip, 1, "stop", 25)


def check_stop_signal():
    return utils.check_redis("localhost", "stop")


def stop():
    set_stop_signal("localhost")


def stop_one(ip):
    set_stop_signal(ip)
